﻿#ifndef GENERATEREPORT_GENERATEREPORTBASE_H
#define GENERATEREPORT_GENERATEREPORTBASE_H

#include <QList>
#include <QString>
#include <QMap>
#include <com/sun/star/uno/Reference.hxx>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/text/XTextDocument.hpp>

using namespace com::sun::star;

namespace GenerateReport
{
    class MainWindow;
}
class QWidget;

#define  GenerateReportBaseInstance GenerateReport::GenerateReportBase::getinstance()
namespace GenerateReport
{
    class GenerateReportBase
    {
    public:
        //brief    :  获取单例
        //return   :  _instance
        static GenerateReportBase* getinstance();

        //初始化
        void init(GenerateReport::MainWindow* mainWindow);
        void initUno();

        uno::Reference<uno::XComponentContext> getXContext();
        uno::Reference<frame::XDesktop2> getDesktop();

        GenerateReport::MainWindow* getMainWindow();

        void setXComponent(uno::Reference<lang::XComponent> xComponent);
        uno::Reference<lang::XComponent> getXComponent();

        uno::Reference<frame::XComponentLoader> getXComponentLoader();

        void setTextDocument(uno::Reference<text::XTextDocument> xTextDocument);
        uno::Reference<text::XTextDocument> getTextDocument();

        /// @brief 设置当前报告的临时目录
        /// @param filePath 临时目录
        void setCurrentTempDocumentFilePath(std::string filePath);
        std::string getCurrentTempDocumentFilePath();

        void setCurrentReportName(QString reportName);
        QString getCurrentReportName();

        void setCurrentReportNumber(int currentReportNum);
        int getCurrentReportNumber();

        /// @brief 设置是否是截图时创建报告，并将截图插入报告
        /// @param isTrue 
        void setCreateAndScreenshotToReport(bool isTrue);
        bool getCreateAndScreenshotToReport();

        void appendDocWindowMap(qint64 id, uno::Reference<awt::XWindow> window);
        uno::Reference<awt::XWindow> getDocWindowMap(qint64 id);
        void setDocWindowVisible(qint64 id, bool isVisible);
        void removeDocWindow(qint64 id);
       
        /// @brief 报告记录预览文档
        /// @param fileFullPath 报告路径
        /// @return 窗口ID+时间戳（用于匹配窗口）
        QPair<qint64, qint64> openFile(std::string fileFullPath);

        QPair<qint64, qint64> createBlankDocument();

        QPair<qint64, qint64> LoadDocumentWindow(rtl::OUString documentPath, uno::Sequence<beans::PropertyValue> loadProps, bool isCreateBlankDocument);


        void appendDisplayReportWidget(QString reportName, QWidget* reportWidget);
        void removeDisplayReportWidget(QString reportName);
        QMap<QString, QWidget*> getDisplayReportWidgetMap();
        /// @brief 查看报告窗口是否是打开状态
        /// @param reportName 报告名称
        /// @return true-打开，false-未打开
        bool checkReportWidgetIsOpen(QString reportName);

        void appendWindowComponentMap(qint64 id, uno::Reference<lang::XComponent> xComponent);

    private:
        GenerateReportBase();
        ~GenerateReportBase() = default;

    private:
        static GenerateReportBase* m_instance;
        GenerateReport::MainWindow* m_mainWindow{};
        uno::Reference<uno::XComponentContext> m_xContext;
        uno::Reference<frame::XDesktop2> m_XDesktop{};
        uno::Reference<lang::XComponent> m_xComponent;
        uno::Reference<frame::XComponentLoader> m_xComponentLoader;
        uno::Reference<text::XTextDocument> m_xTextDocument;
        int m_currentReportNum{ 0 };
        std::string m_currentTempDocumentFilePath{};
        QString m_reportName{};
        bool m_isCreateAndScreenshotToReport{ false };
        QMap<qint64, uno::Reference<awt::XWindow>> m_docWindowMap; //文档对应的窗体map
        QMap<QString, QWidget*> m_displayWidgetMap; //文档对应的窗体map<窗口名称，qwidgetIndex>
        QMap<qint64, uno::Reference<lang::XComponent>> m_windowComponentMap; //文档对应的窗体map<窗口名称，qwidgetIndex>

    };

}

#endif // GENERATEREPORT_GENERATEREPORTBASE_H
